﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Tesseract;
using ZXing;
using ZXing.Common;

namespace OCR
{
    public static class OCR
    {
        public static string FontFolderPath = Path.Combine(Application.StartupPath, "tessdata");
        //配置信息文件路径
        public static string ConfigFilePath = Path.Combine(Application.StartupPath, "OrcConfig.json");
        //历史识别记录文件路径
        public static string HistoryFilePath = Path.Combine(Application.StartupPath, "history", "history.json");
        //识别结果存储根文件夹
        public static string SaveRootFolder = Path.Combine(Application.StartupPath, "history");
        public static string MachineCodeFile = Path.Combine(Application.StartupPath, "HardwareID.txt");
        /// <summary>
        /// 字符识别
        /// </summary>
        /// <param name="image">图像</param>
        /// <param name="fontPath">字体目录</param>
        /// <param name="font">字体名称</param>
        /// <param name="recognitionStr">识别字符串范围</param>
        /// <returns>识别结果</returns>
        public static string CharRecognition(Bitmap image, string fontPath, string font, string recognitionStr)
        {

            Tesseract.TesseractEngine noEngine = new Tesseract.TesseractEngine(fontPath, font);
            noEngine.SetVariable("tessedit_char_whitelist", recognitionStr);
            Page noPage = noEngine.Process(image, noEngine.DefaultPageSegMode);
            //float abc = noPage.GetMeanConfidence();
            //ResultIterator a = noPage.GetIterator();
            //a.Begin();
            //do
            //{
            //    float con = a.GetConfidence(PageIteratorLevel.Symbol);
            //    string value = a.GetText(PageIteratorLevel.Symbol);
            //} while (a.Next(PageIteratorLevel.Symbol));
            return noPage.GetText().Trim().Replace(" ", "");
        }

        /// <summary>
        /// 条码码识别
        /// </summary>
        public static string BarCodeRecognition(Bitmap image)
        {
            try
            {
                BarcodeReader reader = new BarcodeReader();
                reader.Options.CharacterSet = "UTF-8";
                //DecodingOptions decoding = new DecodingOptions();
                //decoding.CharacterSet = "UTF-8";
                //// decoding.PossibleFormats.Add(BarcodeFormat.CODE_39);
                ////211232为code128C编码前6位
                //decoding.PossibleFormats.Add(BarcodeFormat.CODE_128);
                // reader.Options = decoding;
                Result result = reader.Decode(image);
                if (result != null)
                {
                    return result.Text;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                return "";
            }

        }
        /// <summary>
        /// 区域转换
        /// </summary>
        /// <param name="rectangle">原区域</param>
        /// <param name="widthRate">宽度比例</param>
        /// <param name="heightRate">高度比例</param>
        /// <param name="trans">true:还原;false:变形</param>
        /// <returns></returns>
        public static Rectangle RectangleChange(Rectangle rect, double widthRate, double heightRate, bool trans)
        {
            Rectangle returnRec = new Rectangle();
            if (trans)//按比例还原
            {
                returnRec.X = Convert.ToInt32(rect.X / widthRate);
                returnRec.Y = Convert.ToInt32(rect.Y / heightRate);
                returnRec.Width = Convert.ToInt32(rect.Width / widthRate);
                returnRec.Height = Convert.ToInt32(rect.Height / heightRate);

            }
            else//按比例变形
            {
                returnRec.X = Convert.ToInt32(rect.X * widthRate);
                returnRec.Y = Convert.ToInt32(rect.Y * heightRate);
                returnRec.Width = Convert.ToInt32(rect.Width * widthRate);
                returnRec.Height = Convert.ToInt32(rect.Height * heightRate);
            }
            return returnRec;

        }
        public static string TimeSpan(DateTime DateTime1, DateTime DateTime2, 分秒毫秒 format)
        {
            string dateDiff = "";
            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);

            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);

            TimeSpan ts = ts1.Subtract(ts2).Duration();
            if (format == 分秒毫秒.分秒)
            {
                dateDiff = ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
            }
            else if (format == 分秒毫秒.秒毫秒)
            {
                dateDiff = ts.Seconds.ToString() + "秒" + ts.Milliseconds.ToString() + "毫秒";
            }
            return dateDiff;
        }
    }
}
